package com.gcd.alienandwheel.actor;

import com.badlogic.gdx.graphics.Pixmap;
import com.gcd.alienandwheel.MyUtil;

/**
 *
 * @author Rezan Achmad
 */
public class Track2 extends MyActor {

    public float speedForward = 0;
    public float speedBackward = 50;
    public float speedDecay = 0.98f;
    public float acc = 300;
    public float maxSpeed = 500;
    public float milis = 0;
    public float rotInc = 10;
    public Pixmap pixmapMapModel;
    public Pixmap grass;
    public Pixmap ground;
    public Pixmap bufferGround;
    int numberX;
    int numberY;

    public Track2(String name) {
        super(name);
        pixmapMapModel = new Pixmap(MyUtil.loadImage("map-model-2-0.png"));
//        pixmapMapModel = new Pixmap(1024, 2048, Pixmap.Format.RGBA8888);
//        pixmapMapModel.setColor(0, 0, 0, 1);
//        pixmapMapModel.fillRectangle(0, 0, 1024, 2048);
//        pixmapMapModel.setColor(1, 1, 1, 1);
//        pixmapMapModel.fillRectangle(256, 0, 512, 2048);
        
        grass = new Pixmap(MyUtil.loadImage("grass.png"));
        ground = new Pixmap(MyUtil.loadImage("ground.png"));
        bufferGround = new Pixmap(pixmapMapModel.getWidth(), pixmapMapModel.getHeight(), Pixmap.Format.RGBA8888);

        int tiledWidth = ground.getWidth();
        int tiledHeight = ground.getHeight();

        numberX = pixmapMapModel.getWidth() / tiledWidth;
        numberY = pixmapMapModel.getHeight() / tiledHeight;
        System.out.println(numberX);
        System.out.println(numberY);

        for (int i = 0; i < numberX; ++i) {
            for (int j = 0; j < numberY; ++j) {
                bufferGround.drawPixmap(ground, i * tiledWidth, j * tiledHeight, 0, 0, tiledWidth, tiledHeight);
            }
            //System.out.println(i);
        }

        int c;
        float r;
        float g;
        float b;
        float a;
        
        for (int i = 0; i < pixmapMapModel.getWidth(); ++i) {
            for (int j = 0; j < pixmapMapModel.getHeight(); ++j) {
                if (pixmapMapModel.getPixel(i, j) != -1) {
                    c = grass.getPixel(i % tiledWidth, j % tiledHeight);
                    a = (c % 256) / 255f; c = c >> 8;
                    r = (c % 256) / 255f; c = c >> 8;
                    g = (c % 256) / 255f; c = c >> 8;
                    b = (c % 256) / 255f;
                    //System.out.println(c + " " + r + " " + g + " " + b + " " + a);
                    bufferGround.setColor(r, g, b, a);
                    bufferGround.drawPixel(i, j);
                }
            }
        }
        
        setTexture(bufferGround);
    }
}
